package org.kl.bf.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;

import org.kl.bf.entity.basic.Config;
import org.kl.bf.service.basic.ConfigService;
import org.kl.bf.utils.BasicConstants;
import org.kl.wx.entity.App;
import org.kl.wx.service.AppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 初始化系统配置
 * 
 * @author hqm
 * @email hqm1988.good@163.com
 * @createTime 2012-5-23 下午11:52:55
 * 
 */
public class WebContext implements ServletContextListener {
	private static Logger logger = LoggerFactory.getLogger(WebContext.class);

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		logger.info("开始初始化系统配置.........");
		ConfigService configService = (ConfigService) WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(
				"configService");
		List<Config> configList = configService.getAll();
		for (Config config : configList) {
			logger.info(config.toString());
			sce.getServletContext().setAttribute(config.getConfigNo(), config.getConfigValue());
		}
		logger.info("成功初始化系统配置.........");

		logger.info("开始初始化微信公众号配置.........");
		AppService appService = (AppService) WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(
				"appService");
		List<App> appList = appService.getAllApp();
		Map<String, WxMpService> appId_wxMpServiceMap = new HashMap<String, WxMpService>();
		Map<String, App> appId_wxMpAppMap = new HashMap<String, App>();
		Map<String, WxMpService> token_wxMpServiceMap = new HashMap<String, WxMpService>();
		Map<String, App> token_wxMpAppMap = new HashMap<String, App>();
		for (App app : appList) {
			WxMpService wxMpService = new WxMpServiceImpl();
			wxMpService.setWxMpConfigStorage(app);
			appId_wxMpServiceMap.put(app.getAppId(), wxMpService);
			appId_wxMpAppMap.put(app.getAppId(), app);
			token_wxMpServiceMap.put(app.getToken(), wxMpService);
			token_wxMpAppMap.put(app.getToken(), app);
		}
		sce.getServletContext().setAttribute(BasicConstants.SC_WXMPSERVICE_APPID_MAP, appId_wxMpServiceMap);
		sce.getServletContext().setAttribute(BasicConstants.SC_WXAPP_APPID_MAP, appId_wxMpAppMap);
		sce.getServletContext().setAttribute(BasicConstants.SC_WXMPSERVICE_TOKEN_MAP, token_wxMpServiceMap);
		sce.getServletContext().setAttribute(BasicConstants.SC_WXAPP_TOKEN_MAP, token_wxMpAppMap);
		appList = null;
		logger.info("成功初始化微信公众号配置.........");

		Resource resource = new ClassPathResource("/application.properties");
		try {
			Properties props = PropertiesLoaderUtils.loadProperties(resource);
			if (props != null) {
				sce.getServletContext().setAttribute(BasicConstants.JDBC_DRIVER, props.get(BasicConstants.JDBC_DRIVER));
				sce.getServletContext().setAttribute(BasicConstants.JDBC_URL, props.get(BasicConstants.JDBC_URL));
				sce.getServletContext().setAttribute(BasicConstants.JDBC_USERNAME, props.get(BasicConstants.JDBC_USERNAME));
				sce.getServletContext().setAttribute(BasicConstants.JDBC_PASSWORD, props.get(BasicConstants.JDBC_PASSWORD));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	}

}
